package com.tanhua.sso.service;

import com.tanhua.sso.config.TencentSMSConfig;
import com.tanhua.sso.vo.ErrorResult;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.sms.v20210111.SmsClient;
import com.tencentcloudapi.sms.v20210111.models.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.Duration;

@Service
@Slf4j
public class SmsService {
    @Autowired
    private TencentSMSConfig tencentSMSConfig;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    public String sendSms(String mobile) {
        Credential cred = new Credential(this.tencentSMSConfig.getSecretId(), this.tencentSMSConfig.getSecretKey());

        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint("sms.tencentcloudapi.com");

        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);

        SmsClient client = new SmsClient(cred, this.tencentSMSConfig.getRegionId(), clientProfile);

        String code = RandomUtils.nextInt(100000, 999999) + "";

        SendSmsRequest req = new SendSmsRequest();
        String[] phoneNumberSet = {"+86" + mobile};
        req.setPhoneNumberSet(phoneNumberSet);
        String[] templateParamSet = {code, "1"};
        req.setTemplateParamSet(templateParamSet);

        req.setSmsSdkAppId(this.tencentSMSConfig.getSmsSdkAppId());
        req.setTemplateId(this.tencentSMSConfig.getTemplateId());

        req.setSignName("斗图表情包大全精选");
        try {
            SendSmsResponse resp = client.SendSms(req);
            System.out.println(SendSmsResponse.toJsonString(resp));
            String[] split = SendSmsResponse.toJsonString(resp).split(",");
            for (String s : split) {
                if (s.contains("Ok")) {
                    return code;
                }
            }
            log.info("发送短信验证码失败~ data = " + SendSmsResponse.toJsonString(resp));
        } catch (Exception e) {
            log.error("发送短信验证码失败~ mobile = " + mobile, e);
        }
        return null;
    }

    public ErrorResult sendCheckCode(String phone) {
        String redisKey = "CHECK_CODE_" + phone;
        if (redisTemplate.hasKey(redisKey)) {
            String msg = "上一次发送的验证码还未失效！";
            return ErrorResult.builder().errCode("000000").errMessage(msg).build();
        }
        String code = this.sendSms(phone);
//        String code = "123456";
        if (StringUtils.isEmpty(code)) {
            String msg = "发送短信验证码失败！";
            return ErrorResult.builder().errCode("000000").errMessage(msg).build();
        }
        this.redisTemplate.opsForValue().set(redisKey, code, Duration.ofMinutes(5));
        return null;
    }
}
